package com.zmn.oms.third.taobao;

import com.zmn.base.common.data.common.constant.TagConsts;
import com.zmn.base.common.data.common.dio.tags.TagsDIO;
import com.zmn.base.common.data.common.dro.tags.TagsDRO;
import com.zmn.base.common.data.common.query.tags.TagsQuery;
import com.zmn.base.common.data.dubbo.interfaces.tags.TagsListRemoteService;
import com.zmn.base.common.data.dubbo.interfaces.tags.TagsModifyRemoteService;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.third.taobao.dto.TaobaoAccessToken;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 类描述：
 *
 * @author LiangHailong
 * @date 2019/06/22 17:59
 */
@Component("taobaoAccessTokenService")
public class TaobaoAccessTokenService {
    private static final Logger logger = LoggerFactory.getLogger(TaobaoAccessTokenService.class);

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    TagsListRemoteService tagsListRemoteService;
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    TagsModifyRemoteService tagsModifyRemoteService;

    /**
     * 查token
     * @return
     */
    public TagsDRO queryToken() {
        TagsDRO tagsDRO = null;
        TagsQuery tagsDIO = new TagsQuery();
        tagsDIO.setOneTypeId(TagConsts.TAG_ONE_TYPE_OMS_CHANNEL_TOKEN);
        tagsDIO.setTwoTypeId(OrderConsts.CHANNEL_ID_TMALL_WYG);
        tagsDIO.setPlat(GlobalConsts.PLAT_MARK_YEYX);
        ResponseDTO<List<TagsDRO>> listResponseDTO = tagsListRemoteService.listByTagsDIO(tagsDIO);
        if (listResponseDTO.isSuccess()) {
            if (CollectionUtil.isNotNullOrEmpty(listResponseDTO.getData())) {
                tagsDRO = listResponseDTO.getData().get(0);
            }
        }
        if (tagsDRO == null) {
            logger.warn("【{}】<queryToken> Result:[{}]", TaobaoTmcConstants.NAME_TMALL, tagsDRO.toString());
        }

        return tagsDRO;
    }

    /**
     * 保存token
     * @param token
     * @return
     */
    public boolean saveToken(TaobaoAccessToken token) {
        logger.info("【{}】<saveToken> TaobaoAccessToken:[{}]", TaobaoTmcConstants.NAME_TMALL, (token == null ? "null" : token.toString()));
        if (token == null) {
            return false;
        }

        TagsDIO baseCodeMapDIO = new TagsDIO();
        baseCodeMapDIO.setStatus(GlobalConsts.STATUS_NORMOL);
        baseCodeMapDIO.setPlat(GlobalConsts.PLAT_MARK_YEYX);
        baseCodeMapDIO.setOneTypeId(TagConsts.TAG_ONE_TYPE_OMS_CHANNEL_TOKEN);
        baseCodeMapDIO.setTwoTypeId(OrderConsts.CHANNEL_ID_TMALL_WYG);
        baseCodeMapDIO.setName(token.toString());

        // 查询是否有过期token，有的话就修改，没有就新增
        // 新增
        TagsDRO tagsDRO = queryToken();
        ResponseDTO<TagsDRO> response = null;
        if (tagsDRO == null) {
            response = tagsModifyRemoteService.addTagsDIO(baseCodeMapDIO);
        }
        // 修改
        else {
            baseCodeMapDIO.setTagId(tagsDRO.getTagId());
            response = tagsModifyRemoteService.modifyById(baseCodeMapDIO);
        }

        if (response == null || !response.isSuccess()) {
            logger.warn("【{}】<saveToken> Result:[{}]", TaobaoTmcConstants.NAME_TMALL, response);
            return false;
        }

        return true;
    }

}
